const pomelo = window.pomelo;
let connHost;   //connector地址
let connPort;   //connector端口
let isConnectorEntry=false;    //是否连接Connector
let testUid = "test2"

//用户登陆
async function login(elem) {
  let route = "login.loginHandler.login";

  if(isConnectorEntry){   //重新登陆
    pomelo.disconnect();
    isConnectorEntry = false;
  }

  if(!isConnectorEntry)   //初始连接
    await connectorEntry(document.getElementById("connBtn"));

  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      if(data.code !== 200) return;
      showLog("用户登陆成功");
      console.log(data);
      elem.setAttribute("value", "重新登陆");
      resolve();
    });
  });
}

//-------------------------------------

//打开交易所
async function openExchange(){
  let route = "exchange.exchangeHandler.openExchange";
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      showLog("打开交易所");
      //rank 用户等级  tax 交易扣税  lateFee 取消交易违约金  
      //exchangeFrom 交易表单信息{ id(表单ID), house(交易所), created_at(创建时间) }
      //tokyo 东京  london 伦敦  newYork 纽约
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
    });
  });
}

//我要交易
async function startExchange(){
  let route = "exchange.exchangeHandler.startExchange";
  return new Promise( resolve => {
    //house 交易所名称
    pomelo.request(route, {house: "东京交易所"} , async data => {
      showLog("我要交易");
      //oilNum 可交易石油  oilNumList 交易数量列表 (交易所信息由客户端获取)
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
    });
  });
}

//立即交易
async function immediatelyExchange(){
  let route = "exchange.exchangeHandler.immediatelyExchange";
  return new Promise( resolve => {
    //num 客户端选择的交易石油数量
    pomelo.request(route, {num: 1, house: "tokyo"} , data => {
      showLog("立即交易");
      showLog(JSON.stringify(data));
    });
  });
}

//交易完成
async function finishExchange(){
  let route = "exchange.exchangeHandler.finishExchange";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog("交易完成");
      //oilNum 交易数量  money 可得金额  tax 交易扣税  yesterdayPrice 昨日成交价
      showLog(JSON.stringify(data));
    });
  });
}

//确认交易
async function confirmExchange(){
  let route = "exchange.exchangeHandler.confirmExchange";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog("确认交易");
      showLog(JSON.stringify(data));
    });
  });
}

//取消交易
async function cancelExchange(){
  let route = "exchange.exchangeHandler.cancelExchange";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog("取消交易");
      showLog(JSON.stringify(data));
    });
  });
}


//-------------------------------------

//连接Connector
async function connectorEntry(elem) {
  let route = "connector.entryHandler.entry";
  if(!connHost || !connPort) 
    await gateConnector(document.getElementById("gateBtn"));
  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.init({ host: connHost, port: connPort, log: true }, function() {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("初始化connector成功");
        elem.setAttribute("disabled","");
        isConnectorEntry = true;
        resolve();
      });
    });
  });
}

//Gate获取Connector端口地址
function gateConnector(elem) {
  let route = "gate.gateHandler.entry";
  let gateHost = "127.0.0.1";
  let gatePort = 3110;
  return new Promise( resolve => {
    pomelo.init({ host: gateHost, port: gatePort, log: true }, function(err) {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("Gate获取Connector端口地址");
        showLog(JSON.stringify(data));
        if(!data || data.code != 200) return;
        elem.setAttribute("disabled","");
        connHost = data.host;
        connPort = data.port;
        resolve();
      });
    });
  });
}

//显示日志
function showLog(log){
  let logPanel = document.getElementById("showLog");
  let textobj=document.createTextNode(log+"\n");
  logPanel.prepend(textobj);
}
